package cn.jn.lhm.statistics.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.jn.lhm.statistics.clent.UcClient;
import cn.jn.lhm.statistics.entity.StatisticsDaily;
import cn.jn.lhm.statistics.entity.vo.EchartsVo;
import cn.jn.lhm.statistics.mapper.StatisticsMapper;
import cn.jn.lhm.statistics.service.StatisticsService;
import cn.jn.lhm.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/18 - 18:46
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private StatisticsMapper statisticsMapper;

    @Resource
    private UcClient ucClient;

    @Override
    public void countRegisterNumber(String day) {
        //删除原来的数据，保证每天只有一条数据存在
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        statisticsMapper.delete(wrapper);

        //然后进行数据的添加
        R r = ucClient.countRegisterNumber(day);
        Integer number = (Integer)r.getData().get("data");
        //添加数据到数据库中
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(number)
                .setDateCalculated(day)
                .setVideoViewNum(RandomUtil.randomInt(100, 200))
                .setLoginNum(RandomUtil.randomInt(100, 200))
                .setCourseNum(RandomUtil.randomInt(100, 300));
        statisticsMapper.insert(daily);
    }

    @Override
    public Map<String, Object> showData(EchartsVo echartsVo) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.select("date_calculated",echartsVo.getType());
        if (!StrUtil.hasEmpty(echartsVo.getBegin())){
            wrapper.ge("date_calculated", echartsVo.getBegin());
        }
        if (!StrUtil.hasEmpty(echartsVo.getEnd())){
            wrapper.le("date_calculated", echartsVo.getEnd());
        }
        //封装数据
        List<StatisticsDaily> list = statisticsMapper.selectList(wrapper);
        ArrayList<String> dateList = new ArrayList<>();
        ArrayList<Integer> numList = new ArrayList<>();
        //将返回的数据中的数据进行一一封装
        list.forEach(sta -> {
            dateList.add(sta.getDateCalculated());
            assert echartsVo != null;
            switch (echartsVo.getType()){
                case "register_num" :numList.add(sta.getRegisterNum()); break;
                case "login_num" :numList.add(sta.getLoginNum()); break;
                case "video_view_num" :numList.add(sta.getVideoViewNum()); break;
                case "course_num" :numList.add(sta.getCourseNum()); break;
                default: break;
            }
        });
        //返回map
        Map<String, Object> map = new HashMap<>();
        map.put("dateList", dateList);
        map.put("numList", numList);
        return map;
    }
}
